﻿using System;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace CalculoPreciosUnitarios.Logica.GestorObject.Porcentuales_Cantidades
{
    public class CargaPorcentualesCantidadesCS : INotifyPropertyChanged, IDataErrorInfo
    {
        private string codigoArticulo;
        private Int16 cantidadArticulos;
        private decimal manoDeObra;
        private decimal gastosFijos;
        private decimal rentabilidad;
        private decimal insumos;
        private decimal iva;

        public event PropertyChangedEventHandler PropertyChanged;
        private IFormatProvider english = new System.Globalization.CultureInfo("es-AR");


        public string CodigoArticulo
        {
            get
            {
                return codigoArticulo;
            }
            set
            {
                codigoArticulo = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("CodigoArticulo"));
            }
        }

        public Int16 CantidadArticulos
        {
            get
            {
                return cantidadArticulos;
            }
            set
            {
                cantidadArticulos = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("CantidadArticulos"));
            }
        }

        public decimal ManoDeObra
        {
            get
            {
                return manoDeObra;
            }
            set
            {
                manoDeObra = Convert.ToDecimal(value, english);  
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ManoDeObra"));
            }
        }

        public decimal GastosFijos
        {
            get
            {
                return gastosFijos;
            }
            set
            {
                gastosFijos = Convert.ToDecimal(value, english); ;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("GastosFijos"));
            }
        }

        public decimal Rentabilidad
        {
            get
            {
                return rentabilidad;
            }
            set
            {
                rentabilidad = Convert.ToDecimal(value, english); ;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Rentabilidad"));
            }
        }

        public decimal Insumos
        {
            get
            {
                return insumos;
            }
            set
            {
                insumos = Convert.ToDecimal(value, english); ;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Insumos"));
            }
        }

        public decimal Iva
        {
            get
            {
                return iva;
            }
            set
            {
                //iva = Convert.ToDecimal(value, english); ;
                if (iva == value)
                    return;
                iva = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Iva"));
            }
        }

        public string Error
        {
            get
            {
                return null; 
            }
        }

        public string this[string propertyName]
        {
            get
            {
                return IsValid(propertyName);
            }
        }

        private string IsValid(string propertyName)
        {
            switch (propertyName)
            {
                case "CantidadArticulos":
                    if (string.IsNullOrWhiteSpace(CantidadArticulos.ToString()))
                        return "El campo Cantidad de Articulos es requerido - ej: 100";
                    else
                    {
                        Regex regEx;
                        regEx = new Regex(@"^[0-9]{1,4}$");
                        if (!regEx.IsMatch(CantidadArticulos.ToString()))
                            return "El campo Cantidad de Articulos no " +
                                   "tiene un formato correcto \n ej: 100";
                    }
                    break;

                case "Iva":
                    if (string.IsNullOrWhiteSpace(Iva.ToString()))
                        return "El campo Iva es requerido \n ej: 21,00";
                    else
                    {
                        Regex regEx;
                        regEx = new Regex(@"^[0-9]{1,3}\,[0-9]{1,2}$");
                        if (!regEx.IsMatch(Iva.ToString()))
                            return "El campo Iva no " +
                                   "tiene un formato correcto \n ej: 21,00";
                    }
                    break;


            }
            return null;
        }

        public bool IsValid()
        {
            return string.IsNullOrEmpty(IsValid("CantidadArticulos"))
                && string.IsNullOrEmpty(IsValid("Iva"));
        }

    }
}
